#include<bits/stdc++.h>
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define mid ((l+r)>>1)
using namespace std;
namespace IO{
    char buff[1<<21],*p1=buff,*p2=buff;
    char getch(){
        return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
    }
    template<typename T>
    void read(T &x){
        char ch=getch();int fl=1;x=0;
        while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
        x*=fl;
    }
    template<typename T,typename ...Args>
    void read(T &x,Args& ...args){
        read(x);read(args...);
    }
    char obuf[1<<21],*p3=obuf;
    void putch(char ch){
        if(p3-obuf<(1<<21))*p3++=ch;
        else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
    }
    void pc(const char *s){
    	while(*s)putch(*s),s++;
    }
    char ch[100];
    template<typename T>
    void write(T x){
        if(!x)return putch('0');
        if(x<0)putch('-'),x*=-1;
        int top=0;
        while(x)ch[++top]=x%10+48,x/=10;
        while(top)putch(ch[top]),top--;
    }
    template<typename T,typename ...Args>
    void write(T x,Args ...args){
        write(x),putch(' '),write(args...);
    }
    void flush(){fwrite(obuf,p3-obuf,1,stdout);p3=obuf;}
}
using namespace IO;
const int N=4005,B=16,mod=1e9-1;
int n,q;
ll a[N][N],s[N/B+5][N/B+5],s1[N/B+5][N/B+5][B],s2[N/B+5][N/B+5][B];
void Hang(int x,int y,int ops){
	int xid=x/B,x_=x%B;
	for(int i=0,j=0;i<n;i=j+1){
		int yid=i/B;
		while(j+1<n&&yid==(j+1)/B)j++;
		ll det=0;
		for(int k=i,_=0;k<=j;k++,_++){
			det-=a[x][k];
			if(ops==1)a[x][k]&=y;
			if(ops==2)a[x][k]|=y;
			if(ops==3)a[x][k]^=y;
			if(ops==4)a[x][k]=(a[x][k]+y)%mod;
			det+=a[x][k];
			s2[xid][yid][_]+=det;
		}
		for(int k=x_;k<B;k++)
			s1[xid][yid][k]+=det;
		s[xid][yid]+=det;
	}
}
void Lie(int x,int y,int ops){
	int yid=x/B,y_=x%B;
	for(int i=0,j=0;i<n;i=j+1){
		int xid=i/B;
		while(j+1<n&&xid==(j+1)/B)j++;
		ll det=0;
		for(int k=i,_=0;k<=j;k++,_++){
			det-=a[k][x];
			if(ops==1)a[k][x]&=y;
			if(ops==2)a[k][x]|=y;
			if(ops==3)a[k][x]^=y;
			if(ops==4)a[k][x]=(a[k][x]+y)%mod;
			det+=a[k][x];
			s1[xid][yid][_]+=det;
		}
		for(int k=y_;k<B;k++)
			s2[xid][yid][k]+=det;
		s[xid][yid]+=det;
	}
}
ll calc(int x,int y){
	ll ans=0;
	int xid=x/B,yid=y/B;
	int x_=x%B,y_=y%B;
	for(int i=0;i<xid;i++)
		for(int j=0;j<yid;j++)
			ans+=s[i][j];
	for(int i=0;i<xid;i++)
		ans+=s2[i][yid][y_];
	for(int i=0;i<yid;i++)
		ans+=s1[xid][i][x_];
	for(int i=xid*B;i<=x;i++)
		for(int j=yid*B;j<=y;j++)
			ans+=a[i][j];
	return ans;
}
signed main(){
	freopen("fire.in","r",stdin);
	freopen("fire.out","w",stdout);
	read(n,q);
	while(q--){
		int _;
		read(_);
		if(_==1){
			int o1,x,o2,y;
			read(o1,x,o2,y);
			x--;
			if(o1==0)Hang(x,y,o2);
			else Lie(x,y,o2);
		}else{
			int x,y;
			read(x,y);
			x--,y--;
			write(calc(x,y)),putch('\n');
		}
	}
	return flush(),0;
}
